<?php
namespace Home\Controller;
use Common\Common\Classes\Message;
use Home\Model\ClassService;
use Home\Model\ClassStudentService;
use Home\Model\CourseService;
use Home\Model\QuestionSetService;
use Home\Model\StudentService;
use Home\Model\TeacherModel;
use Home\Model\TeacherService;
use Think\Controller;
class TeaClassController extends HomeController{
//---------------------------------获得班级的学生基本信息-----------------------------------------------
    public function classStuInfo(){
        $cla_sql = new ClassService();
        $check = $cla_sql->checkPower();            //查看权限
        if($check->status==1) {
            $cla_stu_sql = new ClassStudentService();
            $applyStu = $cla_stu_sql->getApplyStuInfo();        //获得申请人
            $applyNum = count($applyStu);          //通过申请人数组获得申请人数
            $classInfo = $cla_sql->classInfo();                  //班级的基本信息(包括课程)
            $pageInfo = $cla_sql->getPageInfo();            //获得分页的资料
            $allClass = $cla_sql->getAllClasses(0);             //所有班级需要的信息(导入学生功能)
            $stu_field = array("student_stu_id");
            $student = $cla_stu_sql->getClassStuInfo($pageInfo["np"],$stu_field,0);            //获得班级的学生基本信息(在班级中的)
            $this->assign("allClass",$allClass);
            $this->assign("applyNum", $applyNum);
            $this->assign("applyStu",$applyStu);
            $this->assign("classInfo", $classInfo);
            $this->assign("pageInfo",$pageInfo);
            $this->assign("student",$student);
            $this->display();
        }
        else{
            $this->error($check->message);
        }
    }
    //---------------------------------新增一个班级的方法-----------------------------------------------
    public function addClass(){
        if(IS_AJAX){
            $cla_sql = new ClassService();
            $data = $cla_sql->addClass();
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------删除班级----------------------------------------------
    public function deleteClass(){
        if(IS_AJAX){
            $cla_sql = new ClassService();
            $data = $cla_sql->checkPower();
            if($data->status == 1) {
                $data = $cla_sql->deleteClass();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------修改班级信息----------------------------------------------
    public function updateClass(){
        if(IS_AJAX) {
            $cla_sql = new ClassService();
            $data = $cla_sql->checkPower();
            if ($data->status == 1) {
                $data = $cla_sql->updateClass();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------检测班级是否可以删除-------------------------------------
    public function checkIsDelete(){
        if(IS_AJAX) {
            $cla_sql = new ClassService();
            $data = $cla_sql->checkPower();
            if($data->status == 1) {
                $data = $cla_sql->checkIsDelete();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------批准学生加入到班级中-------------------------------------
    public function allowStuJoin(){
        if(IS_AJAX){
            $cla_stu_sql = new ClassStudentService();
            $data = $cla_stu_sql->checkPower();
            if($data->status == 1) {
                $data = $cla_stu_sql->allowStuJoin();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------从申请中移除学生的申请------------------------------------------
    public function removeApply(){          //有些小BUG要处理
        if(IS_AJAX){
            $cla_stu_sql = new ClassStudentService();
            $data = $cla_stu_sql->checkPower();
            if($data->status == 1) {
                $data = $cla_stu_sql->removeApply();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------从班级中移除学生-----------------------------------------
    public function removeStu(){
        if(IS_AJAX){
            $cla_stu_sql = new ClassStudentService();
            $data = $cla_stu_sql->checkPower();
            if($data->status == 1) {
                $data = $cla_stu_sql->removeStu();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------获得老师的未归档班级----------------------------------------------未归档
    public function classes() {
        $cla_sql = new ClassService();
        $cla_sql->filedClass();                //自动归档处理
        $cou_sql = new CourseService();
        if (count($_GET)!=0) {
            $check = $cou_sql->checkPower();
            if ($check->status == 1) {
                $classes = $cla_sql->getClassByCourse(0);
            }
            else{
                $this->error($check->message);
            }
        } else {
            $classes = $cla_sql->getAllClasses(1,0);
        }
        $qs_sql = new QuestionSetService();
        for($i = 0;$i<count($classes);$i++){            //给每个班级附上最新测试结果
            $_POST["class_cla_id"] = $classes[$i]["cla_id"];
            $classes[$i]["quiz"] = $qs_sql->getTwoOverQuiz();
        }
        $filedClass = $cla_sql->getAllClasses(1,1);
        if(count($filedClass)>0){
            $has_filed = 1;
        }
        else{
            $has_filed = 0;
        }
        $cou_field = array("cou_id","cou_name");
        $courses = $cou_sql->getAllCourse($cou_field);
        $this->assign("has_filed",$has_filed);
        $this->assign("courses", $courses);
        $this->assign("classes",$classes);
        $this->display();
    }
    //---------------------------------获得老师的归档班级-----------------------------------------------
    public function getFiledClass(){
        if(IS_AJAX){
            $cla_sql = new ClassService();
            if($_POST["cd"]!=""){
                $classes = $cla_sql->getClassByCourse(1);
            }
            else{
                $classes = $cla_sql->getAllClasses(1,1);
            }
            $data = new Message(1,"",$classes);
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------恢复班级(从归档变回未归档)---------------------------------------------
    public function  renewClass(){
        if(IS_AJAX){
            $cla_sql = new ClassService();
            $data = $cla_sql->checkPower();
            if($data->status == 1) {
                $data = $cla_sql->renewClass();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------归档班级(从未归档变归档)-----------------------------------------------
    public function fileClass(){
        if(IS_AJAX){
            $cla_sql = new ClassService();
            $data = $cla_sql->checkPower();
            if($data->status == 1) {
                $data = $cla_sql->fileClass();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------根据学号获得某个信息的基本信息-----------------------------------------------
    public function getStuInfo(){
        if(IS_AJAX){
            $cla_stu_sql = new ClassStudentService();
            $check = $cla_stu_sql->checkPower();
            if($check->status == 1) {
                unset($_POST["class_cla_id"]);
                $tea_sql = new TeacherService();
                $tea_field = array("college_col_id");
                $teacher = $tea_sql->getInfo($tea_field);
                $stu_sql = new StudentService();
                $stu_field = array("stu_id", "stu_number", "stu_name", "stu_email");
                $_POST["col_id"] = $teacher["college_col_id"];
                $data = $stu_sql->getOneStuInfo($stu_field);
            }
            else{
                $data = new Message(1,"你没有权限对该班级进行操作","");
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------根据班级查询到该班级的学生-----------------------------------------------
    public function getClassStu(){
        if(IS_AJAX){
            $cla_stu_sql = new ClassStudentService();
            $data = $cla_stu_sql->checkPower();
            if($data->status == 1) {
                $classStu = $cla_stu_sql->getClassStu();         //但不包含已经添加的学生
                $data = new Message(1, "", $classStu);
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------导入学生功能(单个多个均可)-----------------------------------------------
    public function importStu(){
        if(IS_AJAX){
            $cla_stu_sql = new ClassStudentService();
            $data = $cla_stu_sql->checkPower();
            if($data->status == 1) {
                $data = $cla_stu_sql->importStu();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------按照指定关键字搜索学生---------------------------------------------------
    public function searchStu(){
        if(IS_AJAX){
            $cla_stu_sql = new ClassStudentService();
            $data = $cla_stu_sql->checkPower();
            if($data->status == 1) {
                $result = $cla_stu_sql->searchStu();
                $this->ajaxReturn($result);
            }
            else{
                $this->error($data->message);
            }
        }
    }
}